* cd  "/Users/lulupan/Nutstore Files/famine&debt/Data/"
cd "/Users/lulupan/Documents/Project/famine&debt/Data/"
use "mayor_cleaned.dta", clear
  
decode mayor_city_code, gen(citycode_value)
drop mayor_city_code
rename citycode_value mayor_city_code
* br mayor_city_code
ren mayor_bcity_code citycode

merge m:1 citycode using "citydeathrate.dta"
keep if _merge==3
drop _merge
ren citycode mayor_bcity_code
ren mayor_year year
* 2006-2018年地级市有息债务+城投债
ren mayor_city_code citycode
destring citycode , replace
replace citycode = citycode/100
*06-18
merge 1:1 citycode year using "debt.dta"
keep if _merge==3
drop _merge

merge 1:1 citycode year using "controls0914.dta"
keep if _merge==3
drop _merge

* mayor_byear这是出生年份
* 我先按照 jde 那篇论文来设置吧
gen born4961 = 0 
replace born4961 = 1 if mayor_byear>=1949 & mayor_byear<=1961
gen born4958 = (mayor_byear>=1949 & mayor_byear<=1958)
gen born5961 = (mayor_byear>=1959 & mayor_byear<=1961)
ren 死亡率 famineseverity
ren mayor_age age
ren mayor_bcity_code htcode
ren mayor_byear birthyr
gen logcitybond = log(citybond/10+1)
gen interaction = famineseverity * born4961
gen loggdppc = log(gdppc)
gen logfdi = log(fdi)
gen loghs_price = log(hs_price)


gen interaction4958 = famineseverity * born4958
gen interaction5961 = famineseverity * born5961

replace mayor_edu = mayor_edu-1 if mayor_edu>1


global cityofficialcv "mayor_party_school mayor_liberal_arts mayor_social_science mayor_science_engineering mayor_agriculture mayor_medicine mayor_economist mayor_engineer mayor_army mayor_youth_league mayor_gender mayor_nation mayor_edu  mayor_local loggdppc tot_pop"
* scnd_gdp   loggdppc
/*

Variable      Storage   Display    Value
    name         type    format    label      Variable label
--------------------------------------------------------------------------------------------
mayor_party_s~l byte    %21.0g     mayor_party_school
                                              是否是党校教育（是=1，否=0）
mayor_liberal~s byte    %15.0g     mayor_liberal_arts
                                              专业：人文（是=1，否=0）
mayor_social_~e byte    %15.0g     mayor_social_science
                                              专业：社科（是=1，否=0）
mayor_science~g byte    %15.0g     mayor_science_engineering
                                              专业：理工（是=1，否=0）
mayor_agricul~e byte    %9.0g      mayor_agriculture
                                              专业：农科（是=1，否=0）
mayor_medicine  byte    %9.0g      mayor_medicine
                                              专业：医科（是=1，否=0）
mayor_economist byte    %12.0g     mayor_economist
                                              是否是经济师（是=1，否=0）
mayor_engineer  byte    %12.0g     mayor_engineer
                                              是否是工程师（是=1，否=0）
mayor_army      byte    %15.0g     mayor_army
                                              是否入伍（是=1，否=0）
mayor_youth_l~e byte    %18.0g     mayor_youth_league
                                              工作经历：团委（是=1，否=0）
mayor_gender    float   %9.0g      gender1    性别
mayor_nation    float   %12.0g     nation1    民族
mayor_edu       float   %9.0g      edu1       教育
mayor_local     float   %18.0g     mayor_local
                                              市长是否来自省内
loggdppc        float   %9.0g                 
tot_pop         double  %10.0g                Total population, 10 thousand
*/

label var mayor_party_school "mayor_party_school"
label var mayor_liberal_arts "mayor_liberal_arts"
label var mayor_social_science "mayor_social_science"
label var mayor_science_engineering "mayor_science_engineering"
label var mayor_agriculture "mayor_agriculture"
label var mayor_medicine "mayor_medicine"
label var mayor_economist "mayor_economist"
label var mayor_engineer "mayor_engineer"
label var mayor_army "mayor_army"
label var mayor_youth_league "mayor_youth_league"
label var mayor_gender "mayor_gender"
label var mayor_nation "mayor_nation"
label var mayor_edu "mayor_edu"
label var mayor_local "mayor_local"
label var loggdppc "loggdppc"
label var tot_pop "tot_pop"
label var citybond "UCIB Issuance"
label var logcitybond "Log UCIB Issuance"
label var interaction "famineseverity * born4961"
label var interaction4958 "famineseverity * born4958"
label var interaction5961 "famineseverity * born5961"


eststo m_1: reghdfe logcitybond interaction , absorb(i.citycode i.year i.htcode i.birthyr) vce(cl citycode)
 

eststo m_2: reghdfe logcitybond interaction $cityofficialcv, absorb(i.citycode i.year i.htcode i.birthyr) vce(cl citycode)


eststo m_3: reghdfe logcitybond interaction4958 interaction5961 $cityofficialcv, absorb(i.citycode i.year i.htcode i.birthyr) vce(cl citycode)




gen year0_2 = 0
replace year0_2 =1 if birthyr>=1958 & birthyr<=1961
gen year3_5 = 0
replace year3_5 =1 if birthyr>=1954 & birthyr<=1957
gen year6_8 = 0
replace year6_8 =1 if birthyr>=1949 & birthyr<=1953
gen year9_ = 0
replace year9_ =1 if birthyr<=1949 

gen year0_2_fam = year0_2 * famineseverity
gen year3_5_fam = year3_5 * famineseverity
gen year6_8_fam = year6_8 * famineseverity
gen year9_fam = year9_ * famineseverity

label var year0_2_fam "year0_2 * famineseverity"
label var year3_5_fam "year3_5 * famineseverity"
label var year6_8_fam "year6_8 * famineseverity"
label var year9_fam "year9+ * famineseverity"

eststo m_4: reghdfe logcitybond year0_2_fam year3_5_fam year6_8_fam year9_fam $cityofficialcv, absorb(i.citycode i.year i.htcode i.birthyr) vce(cl citycode)



outreg2 [m_1 m_2 m_3 m_4] using "baseline.xlsx", replace tstat bdec(3) tdec(2) label  ///
keep(interaction interaction4958 interaction5961  year0_2_fam year3_5_fam year6_8_fam year9_fam $cityofficialcv) ///
sortvar(interaction interaction4958 interaction5961  year0_2_fam year3_5_fam year6_8_fam year9_fam $cityofficialcv) ///
addtext(City FE, ✓, Year FE, ✓, Cohort FE, ✓, Birthplace FE, ✓)


* sum(log) 即输出一般统计指标命令，一般统计指标包括样本数、中值、标准误、最大值和最小值。
outreg2 using statistics.doc, replace sum(log)  ///
        keep(citybond logcitybond interaction interaction4958 interaction5961 year0_2_fam year3_5_fam year6_8_fam year9_fam $cityofficialcv) ///
		sortvar(citybond logcitybond interaction interaction4958 interaction5961  year0_2_fam year3_5_fam year6_8_fam year9_fam $cityofficialcv) ///
		label  

preserve
reghdfe logcitybond interaction $cityofficialcv, absorb(i.citycode i.year i.htcode i.birthyr) vce(cl citycode)
keep if e(sample)==1	

logout, save("correlations_with_stars") excel replace: pwcorr_a logcitybond interaction interaction4958 interaction5961 year0_2_fam year3_5_fam year6_8_fam year9_fam $cityofficialcv,  format(%6.2f) 

restore


/*
preserve
forvalue i = 1949(1)1969 {
gen by`i' = 0
replace by`i' = 1 if birthyr == `i'
replace by`i' = by`i' * famineseverity
}

gen byb1950 = 0
replace byb1950 = 1 if birthyr <= 1950
replace byb1950 = byb1950 * famineseverity

gen byb1965 = 0
replace byb1965 = 1 if birthyr >= 1965
replace byb1965 = byb1965  * famineseverity

reghdfe logcitybond  byb1950 by1951 by1952 by1953 by1954  by1955  by1956  by1957  o.by1958  by1959  by1960  by1961  by1962  by1963  by1964  byb1965    $cityofficialcv , absorb(i.citycode i.year i.htcode i.birthyr) vce(cl citycode)
	
set scheme cleanplots
coefplot,   ///
keep(by*  ) ///
vertical ///
omitted ///
coeflabels( byb1950=1950    by1951=1951  by1952=1952  by1953=1953  by1954=1954 by1955=1955 by1956=1956 by1957=1957 by1958=1958 by1959=1959 by1960=1960 by1961=1961 by1962=1962 by1963=1963  by1964=1964 byb1965=1965  ) /// 
yline(0,lwidth(vthin) lpattern(solid) lcolor(teal)) ///
ylabel(-0.2(0.1)0.2,labsize(*0.85) angle(0)) xlabel(,labsize(*0.85)) ///
ciopts(recast(rcap)lcol(green) )  ytitle(Coefficients) ///
xtitle(Birth year) levels(90) ///
mcolor(blue)   msymbol(O) msize( small )  /// // 将圆圈改为蓝色
xline(9) 
graph save "parallellog.gph",replace
graph export "parallellog.png", replace
restore
*/


preserve
forvalue i = 1949(1)1969 {
gen by`i' = 0
replace by`i' = 1 if birthyr == `i'
replace by`i' = by`i' * famineseverity
}

gen byb1950 = 0
replace byb1950 = 1 if birthyr <= 1950
replace byb1950 = byb1950 * famineseverity

gen byb1965 = 0
replace byb1965 = 1 if birthyr >= 1965
replace byb1965 = byb1965  * famineseverity

reghdfe logcitybond  byb1950 by1951 by1952 by1953 by1954  by1955  by1956  by1957  o.by1958  by1959  by1960  by1961  by1962  by1963  by1964  byb1965    $cityofficialcv , absorb(i.citycode i.year i.htcode i.birthyr) vce(cl citycode)
	
set scheme cleanplots
coefplot,   ///
keep(by*  ) ///
vertical ///
omitted ///
coeflabels( byb1965= `""-6+" "(1965+)""'    ///
           by1964 = `""-5" "(1964)""'   ///
           by1963 = `""-4" "(1963)""'  ///
           by1962 = `""-3" "(1962)""'  ///
           by1961 = `""-2" "(1961)""'  ///
           by1960 = `""-1" "(1960)""'  ///
           by1959 = `""0" "(1959)""'  ///
           by1958 = `""1" "(1958)""'    ///
           by1957 = `""2" "(1957)""'   ///
           by1956 = `""3" "(1956)""'  ///
           by1955 = `""4" "(1955)""'  ///
           by1954 = `""5" "(1954)""'  ///
           by1953 = `""6" "(1953)""'  ///
           by1952 = `""7" "(1952)""'  ///
           by1951 = `""8" "(1951)""'   ///
           byb1950= `""9+" "(1950-)""'  ) ///
xtitle("Cohort Age in 1959 " "(birth year)", size(medsmall)) ///
yline(0,lwidth(vthin) lpattern(solid) lcolor(teal)) ///
ylabel(-0.2(0.1)0.2,labsize(*0.85) angle(0)) xlabel(,labsize(*0.85)) ///
ciopts(recast(rcap)lcol(green) )  ytitle(Coefficients) ///
levels(90) ///
mcolor(blue)   msymbol(O) msize( small )  /// // 将圆圈改为蓝色
order(byb1965 by1964 by1963 by1962 by1961 by1960 by1959 by1958 by1957 by1956 by1955 by1954 by1953 by1952 by1951 byb1950)  ///
xline(8, lpattern(dash) lcolor(blue%50)) /// // formative years开始
xline(11, lpattern(dash) lcolor(blue%50)) /// // formative years结束
text(0.15 9.4 "Formative Years", size(small))  
graph save "parallellog.gph",replace
graph export "parallellog.png", replace
restore





* robustness-1 
est clear
cap drop logbalance
gen logbalance = log(balance/10+1)
label var logbalance "Log UCIB Balance"

eststo m_1: reghdfe logbalance interaction , absorb(i.citycode i.year i.htcode i.birthyr) vce(cl citycode)

eststo m_2: reghdfe logbalance interaction $cityofficialcv, absorb(i.citycode i.year i.htcode i.birthyr) vce(cl citycode)

eststo m_3: reghdfe logbalance interaction4958 interaction5961 $cityofficialcv, absorb(i.citycode i.year i.htcode i.birthyr) vce(cl citycode)


eststo m_4:reghdfe logbalance year0_2_fam year3_5_fam year6_8_fam year9_fam $cityofficialcv, absorb(i.citycode i.year i.htcode i.birthyr) vce(cl citycode)


outreg2 [m_1 m_2 m_3 m_4] using "robust1.xlsx", replace tstat bdec(3) tdec(2) label  ///
keep(interaction interaction4958 interaction5961  year0_2_fam year3_5_fam year6_8_fam year9_fam  ) ///
sortvar(interaction interaction4958 interaction5961  year0_2_fam year3_5_fam year6_8_fam year9_fam  ) ///
addtext(Controls,✓, City FE, ✓, Year FE, ✓, Cohort FE, ✓, Birthplace FE, ✓)



gen logbonds_jj = log(bonds_jj/10+1)
gen logbonds_pg = log(bonds_pg/10+1)
gen logbonds_hz = log(bonds_hz/10+1)
gen logbonds_by = log(bonds_by/10+1)

label var logbonds_jj "Log UCIB for Infrastructure"

label var logbonds_pg "Log UCIB for Affordable Housing"

label var logbonds_hz "Log UCIB for Debt Repayment"

label var logbonds_by "Log UCIB for Working Capital"


eststo m_1:reghdfe logbonds_jj interaction $cityofficialcv, absorb(i.citycode i.year i.htcode i.birthyr) vce(cl citycode)

eststo m_2:reghdfe logbonds_pg interaction $cityofficialcv, absorb(i.citycode i.year i.htcode i.birthyr) vce(cl citycode)

eststo m_3:reghdfe logbonds_hz interaction $cityofficialcv, absorb(i.citycode i.year i.htcode i.birthyr) vce(cl citycode)
*  .0144721    .006969     2.08   0.039  

eststo m_4: reghdfe logbonds_by interaction $cityofficialcv, absorb(i.citycode i.year i.htcode i.birthyr) vce(cl citycode)

outreg2 [m_1 m_2 m_3 m_4] using "robust2.xlsx", replace tstat bdec(3) tdec(2) label  ///
keep(interaction   ) ///
sortvar(interaction   ) ///
addtext(Controls,✓, City FE, ✓, Year FE, ✓, Cohort FE, ✓, Birthplace FE, ✓)




* Moderating effect

cap drop index  interaction*
gen index =  mayor_youth_league
gen interaction1 = famineseverity * born4961 * index
gen interaction2 = famineseverity * index
gen interaction3 = born4961 * index 
gen interaction4 = famineseverity * born4961


label var interaction1 "famineseverity * born4961 * mayor_youth_league"

eststo m1: reghdfe logcitybond interaction1  interaction2 interaction3 interaction4 famineseverity born4961 index $cityofficialcv, absorb(i.citycode i.year i.htcode i.birthyr) vce(cl citycode)

outreg2 [m1] using "moderating1.xlsx", replace tstat bdec(3) tdec(2) label  ///
keep(interaction1  ) ///
sortvar( interaction1 ) ///
addtext(Controls,✓, City FE, ✓, Year FE, ✓, Cohort FE, ✓, Birthplace FE, ✓)


cap drop index interaction*
gen index =  mayor_army
gen interaction0 = famineseverity * born4961 * index
gen interaction2 = famineseverity * index
gen interaction3 = born4961 * index 
gen interaction4 = famineseverity * born4961

label var interaction0 "famineseverity * born4961 * mayor_army"
eststo m2: reghdfe logcitybond interaction0   interaction2 interaction3 interaction4 famineseverity born4961 index $cityofficialcv, absorb(i.citycode i.year i.htcode i.birthyr) vce(cl citycode)


outreg2 [m2] using "moderating2.xlsx", replace tstat bdec(3) tdec(2) label  ///
keep(interaction0  ) ///
sortvar( interaction0 ) ///
addtext(Controls,✓, City FE, ✓, Year FE, ✓, Cohort FE, ✓, Birthplace FE, ✓)
